package com.fa.wisdom.ui;

import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.coder.zzq.smartshow.dialog.LoadingDialog;
import com.fa.wisdom.R;
import com.fa.wisdom.adapter.QiuzhuAdapter;
import com.fa.wisdom.base.BaseActivity;
import com.fa.wisdom.base.Constant;
import com.fa.wisdom.model.MsgEntity;
import com.fa.wisdom.utils.Loading;
import com.fa.wisdom.utils.SpUtils;
import com.fa.wisdom.utils.TitleBuilder;
import com.google.gson.JsonArray;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

public class QiuzhuJLActivity extends BaseActivity {
    private RecyclerView recyclerView;
    List<MsgEntity> msgEntities = new ArrayList<>();

    @Override
    protected boolean isStatusBarBlack() {
        return true;
    }

    @Override
    public void setListen() {
        new TitleBuilder(this).setTitleText("求助记录")
                .setLeftIcoListening(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        finish();
                    }
                });
    }

    @Override
    public void setTitleBarColor() {

    }

    @Override
    public int intiLayout() {
        return R.layout.activity_qiuzhujl;
    }
    QiuzhuAdapter foundRecycleAdapter;
    private void initAdapter() {
        foundRecycleAdapter = new QiuzhuAdapter(msgEntities);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setAdapter(foundRecycleAdapter);
    }

    @Override
    public void initView() {
        recyclerView = findViewById(R.id.recycle);
        initAdapter();
    }

    @Override
    public void initData() {
        getData();
    }

    //
    private void getData() {
        LoadingDialog dialog = Loading.getInstance().onShowLargeLoading("加载中...");
        dialog.showInActivity(this);
        int userID = SpUtils.getInstance().decodeInt(Constant.User_id, -1);
        String token = SpUtils.getInstance().decodeString(Constant.TOKEN, "");
        OkHttpUtils.get().url("https://br.feiaikeji.cn/index.php/api/alarm_logs/alarmList")
                .addParams("user_id", String.valueOf(userID))
                .addParams("token", token)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                dialog.dismiss();
            }

            @Override
            public void onResponse(String response, int id) {
                dialog.dismiss();
                JSONObject jsonObject1 = JSON.parseObject(response);
                JSONArray jsonArray = jsonObject1.getJSONArray("data");
                if (jsonArray != null && jsonArray.size() > 0) {
                    for (int i = 0; i < jsonArray.size(); i++) {
                        JSONObject jsonObject11 = jsonArray.getJSONObject(i);
                        MsgEntity msgEntity = new MsgEntity();
                        msgEntity.setTitle(jsonObject11.getString("phone"));
                        msgEntity.setTime(jsonObject11.getString("create_time"));
                        msgEntities.add(msgEntity);
                    }
                }
                foundRecycleAdapter.notifyDataSetChanged();
            }
        });
    }
}
